package com.sokal.easycare.modules.forum.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.forum.entity.ForumModule;
import com.sokal.easycare.modules.forum.service.ForumModuleService;

/**
 * 论坛板块Controller
 * @author lihaipei
 * @version 2017-05-08
 */
@Controller
@RequestMapping(value = "${adminPath}/forum/forumModule")
public class ForumModuleController extends BaseController {

	@Autowired
	private ForumModuleService forumModuleService;
	
	@ModelAttribute
	public ForumModule get(@RequestParam(required=false) String id) {
		ForumModule entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = forumModuleService.get(id);
		}
		if (entity == null){
			entity = new ForumModule();
		}
		return entity;
	}
	
	@RequiresPermissions("forum:forumModule:view")
	@RequestMapping(value = {"list", ""})
	public String list(ForumModule forumModule, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ForumModule> page = forumModuleService.findPage(new Page<ForumModule>(request, response), forumModule); 
		model.addAttribute("page", page);
		return "modules/forum/forumModuleList";
	}

	@RequiresPermissions("forum:forumModule:view")
	@RequestMapping(value = "form")
	public String form(ForumModule forumModule, Model model) {
		model.addAttribute("forumModule", forumModule);
		return "modules/forum/forumModuleForm";
	}

	@RequiresPermissions("forum:forumModule:edit")
	@RequestMapping(value = "save")
	public String save(ForumModule forumModule, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, forumModule)){
			return form(forumModule, model);
		}
		forumModuleService.save(forumModule);
		addMessage(redirectAttributes, "保存板块成功");
		return "redirect:"+Global.getAdminPath()+"/forum/forumModule/?repage";
	}
	
	@RequiresPermissions("forum:forumModule:edit")
	@RequestMapping(value = "delete")
	public String delete(ForumModule forumModule, RedirectAttributes redirectAttributes) {
		forumModuleService.delete(forumModule);
		addMessage(redirectAttributes, "删除板块成功");
		return "redirect:"+Global.getAdminPath()+"/forum/forumModule/?repage";
	}

}